package Client.serviceCenter.ZkWatcher;

import Client.cache.serviceCache;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.CuratorCache;
import org.apache.curator.framework.recipes.cache.CuratorCacheListener;

public class watchZk {
    // zk客户端
    private CuratorFramework zkClient;
    // 服务缓存
    private serviceCache serviceCache;

    public watchZk(CuratorFramework zkClient, serviceCache serviceCache) {
        this.zkClient = zkClient;
        this.serviceCache = serviceCache;
    }

    // 监听当前节点和子节点的更新，创建和删除
    public void watchToUpdate(String path) throws InterruptedException {
        /**
         * 监听当前节点和子节点的更新，并在节点变化时更新本地缓存
         * 监听指定路径节点变化，这里监听根目录
         */
        CuratorCache curatorCache = CuratorCache.build(zkClient, "/");

        // 注册监听器，处理节点变化事件
        curatorCache.listenable().addListener(new CuratorCacheListener() {

            /**
             * @param type 事件类型（枚举）
             * @param childData 节点更新前的状态、数据
             * @param childData1 节点更新后的状态、数据
             */
            @Override
            public void event(Type type, ChildData childData, ChildData childData1) {
                switch (type) {
                    // 节点创建
                    case NODE_CREATED:
                        System.out.println("节点创建后:" + childData1.getPath());
                        String[] pathList = pasrePath(childData1);
                        if (pathList.length <= 2)
                            break;
                        else {
                            // "/serviceName/address" 分割后是 "" "serviceName" "address"
                            String serviceName = pathList[1];
                            String address = pathList[2];
                            serviceCache.addServiceToCache(serviceName, address);
                            System.out.println("serviceName: " + serviceName + " address: " + address);
                        }
                        break;
                    // 节点更新
                    case NODE_CHANGED:
                        if (childData.getData() != null) {
                            System.out.println("修改前的数据:" + new String(childData.getData()));
                        } else {
                            System.out.println("节点第一次赋值");
                        }
                        String[] oldPathList = pasrePath(childData);
                        String[] newPathList = pasrePath(childData1);
                        serviceCache.replaceServiceToCache(oldPathList[1], oldPathList[2], newPathList[2]);
                        System.out.println("修改后的数据:" + new String(childData1.getData()));
                        break;
                    // 节点删除
                    case NODE_DELETED:
                        String[] pathList_d = pasrePath(childData);
                        if (pathList_d.length <= 2)
                            break;
                        else {
                            String serviceName = pathList_d[1];
                            String address = pathList_d[2];
                            serviceCache.removeServiceFromCache(serviceName, address);
                        }
                        break;
                    default:
                        break;
                }
            }
        });

        // 开始监听
        curatorCache.start();

    }

    private String[] pasrePath(ChildData childData1) {
        String path = childData1.getPath();
        String[] pathList = path.split("/");
        return pathList;
    }

}
